package com.yloveg.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yloveg.base.BaseController;
import com.yloveg.common.FreemarkUtil;
import com.yloveg.common.FreemarkUtil.OrderStatus;
import com.yloveg.common.PageUtil;
import com.yloveg.common.WlUtil;
import com.yloveg.entity.ShopOrders;
import com.yloveg.entity.ShopWl;
import com.yloveg.entity.ShopWlInfo;
import com.yloveg.service.ShopOrdersService;
import com.yloveg.service.ShopWlInfoService;
import com.yloveg.service.ShopWlService;

@Controller
@RequestMapping({"order"})
public class OrderController extends BaseController { 

	
    @Autowired
    private ShopOrdersService shopOrdersService;
    
    @Autowired
    private ShopWlService shopWlService;
    
    @Autowired
    private ShopWlInfoService shopWlInfoService;
    

    /**
     * 订单列表
     * @param map
     * @return
     */
    @RequestMapping({"orderList"})  
    public String orderList(ModelMap map){
        return "landing/order/orderList";
    }
    @RequestMapping("/queryorderPage")
    @ResponseBody
    public Map<String, Object> queryorderPage(int page, int limit,ShopOrders order){
    	if(this.getSessionShop()!=null&&this.getSessionShop().getId()!=null){
    		order.setShopId(this.getSessionShop().getId());
    	}else{
    		order.setShopId(1100000l);
    	}
    	
        Map<String, Object> map = PageUtil.dateToLayTable(shopOrdersService.queryOrdersPage(page,limit,order));
        return map;
    }
    /**
     * 订单详情
     * @param map
     * @param order
     * @return
     */
    @RequestMapping({"orderDetail"})  
    public String orderDetail(ModelMap map,ShopOrders order){
    	order.setShopId(this.getSessionShop().getId());
    	PageInfo<ShopOrders> pageList = shopOrdersService.queryOrdersPage(1,10,order);
    	List<ShopOrders> tempList = pageList.getList();
    	map.put("order", tempList.get(0));
    	if(tempList.get(0).getCourierNumber()!=null){
    		map.put("isWl", "1");
    		if(tempList.get(0).getOrderStatus().equals(FreemarkUtil.OrderStatus.TOBEEVALUATED.getValue())){
    			ShopWl shopWl = new ShopWl();
    			shopWl.setWlNumber(tempList.get(0).getCourierNumber());
     			shopWl = shopWlService.selectByPrimary(shopWl);
    			if(shopWl!=null){
    				ShopWlInfo info = new ShopWlInfo();
    				info.setWlId(shopWl.getId());
    				List<Map<String,String>> wlList = shopWlInfoService.selectByPrimary(info);
    				Map<String, Object> temp = new HashMap<String, Object>();
    				temp.put("list", wlList);
    				map.put("result", temp);
    			}else{
            		Map<String, Object> wl = WlUtil.getWl(tempList.get(0).getCourierNumber().toString(), tempList.get(0).getCourierCom());
            		map.addAllAttributes(wl);
            		Map<String, Object> temp = (Map<String, Object> ) wl.get("result");
            		ShopWl wlInsert = new ShopWl();
            		wlInsert.setDeliverystatus(temp.get("deliverystatus").toString());
            		wlInsert.setExpname(temp.get("expName").toString());
            		wlInsert.setIssign(temp.get("issign").toString());
            		wlInsert.setType(temp.get("type").toString());
            		wlInsert.setWlNumber(Long.valueOf(temp.get("number").toString()));
            		shopWlService.insertShopWl(wlInsert);
            		List<Map<String,String>> wlList = (List<Map<String, String>>) temp.get("list");
            		ShopWlInfo infoInsert = new ShopWlInfo();
            		infoInsert.setWlId(wlInsert.getId());
            		for (Map<String, String> map2 : wlList) {
            			infoInsert.setStatus(map2.get("status"));
            			infoInsert.setTime(map2.get("time"));
            			shopWlInfoService.insertShopWlInfo(infoInsert);
					}
    			}
    			
    		}else{
        		Map<String, Object> wl = WlUtil.getWl(tempList.get(0).getCourierNumber().toString(), tempList.get(0).getCourierCom());
        		if(wl.get("status").equals("0")){
        			map.addAllAttributes(wl);  
        		}else{
        			Map<String, Object> temp = new HashMap<String, Object>();
    				temp.put("list", null);
    				map.put("result", temp);
        		}
        		 
    		}

    	}else{
    		map.put("isWl", "0");
    	}
        return "landing/order/orderDetail";
    }
    
    /**
     * 
     * @param map
     * @param order
     * @return
     */
    @RequestMapping({"addLogistics"})  
    public String addLogistics(ModelMap map,ShopOrders order){
    	order.setShopId(this.getSessionShop().getId());
    	PageInfo<ShopOrders> pageList = shopOrdersService.queryOrdersPage(1,10,order);
    	List<ShopOrders> tempList = pageList.getList();
    	map.put("order", tempList.get(0));
        return "landing/order/addLogistics";
    }

    @RequestMapping("/saveCourierNumber")
    @ResponseBody
    public Map<String, Object> saveCourierNumber(ShopOrders order){
        Map<String, Object> map = new HashMap<String, Object>();
        order.setOrderStatus(OrderStatus.TOBERECEIVED.getValue());
        shopOrdersService.updateByPrimaryKeySelective(order);
        map.put("code", "1");
        map.put("msg", "操作成功!");
        return map;
    }
	
	

}
